import { _decorator, Component, Node } from 'cc';
import { UIBase } from './UIBase';
import { ModuleMgr } from './Card/ModuleMgr';
import { CardMgr } from './Card/CardMgr';
import { Card } from './Card/Card';
import { Cards } from './Card/Cards';
import { GameData } from './GameData';
import { Emitter } from './Emitter';
const { ccclass, property } = _decorator;

@ccclass('Shop')
export class Shop extends UIBase {
    private cardMgr:CardMgr=null;
    private deleteBtn:Node=null;
    private addBtn:Node=null;
    private confirmBtn:Node=null;
    //确认按钮的开关
    private isconfirm:boolean=false;
    //确认增加或者减少(T为增 F为减)
    private addordelete:boolean=false;
    //缓存牌组(存储增加牌)
    private cardbffer:Card[]=[];
    oninit(...rest: any[]): void {
        this.cardMgr=ModuleMgr.instance.getModuel<CardMgr>("CardMgr");
        this.node.parent.getChildByName("JokerMgr").setSiblingIndex(100);
        this.deleteBtn=this.getNode("_DeleteBtn");
        this.addBtn=this.getNode("_AddBtn");
        this.confirmBtn=this.getNode("_ConfirmBtn");
        //默认失活
        this.confirmBtn.active=false;
        //注册事件
        this.addClickEventL("_ContinueBtn",()=>{
            this.openUI("Select");
            this.closeUI("Shop",true);
            this.node.parent.getChildByName("JokerMgr").active=false;
        })
        this.addClickEventL("_DeleteBtn",()=>{
            this.deleteCard();
        })
        this.addClickEventL("_AddBtn",()=>{
            this.addCard();
        })
        this.addClickEventL("_ConfirmBtn",()=>{
            this.confirm();
        })
    }
    protected update(dt: number): void {
        if(this.isconfirm){
            let handCard=this.node.getChildByName("Cards").children;
            for (let i = 0; i < handCard.length; i++) {
                if (handCard[i].getComponent(Card).istouch) {                    
                    this.confirmBtn.active = true;
                    return;
                }
                this.confirmBtn.active = false;
            }
        }
    }
    confirm(){
        //增
        if (this.addordelete) {
            for(let i=this.cardbffer.length-1;i>=0;i--){
                //将没有被选择的卡牌销毁
                if(!this.cardbffer[i].istouch){
                    this.cardbffer[i].node.destroy();
                    this.cardbffer.splice(i,1);
                    continue;
                }
                //剩余卡牌放回抽牌堆
                ModuleMgr.instance.getModuel<CardMgr>("CardMgr").addnewCard(this.cardbffer[i]);
            }
            //结束关闭确认按钮
            this.confirmBtn.active = false;
            this.isconfirm = false;
            //将Cards删除
            let Cards=this.node.getChildByName("Cards");
            Cards.destroy();
            //激活原本按钮
            this.deleteBtn.active = true;
            this.addBtn.active = true;
            return;
        }
        //减
        let handCard = this.node.getChildByName("Cards").getComponent(Cards).handCard;
        let cards = this.node.getChildByName("Cards");
        this.cardMgr.deleteCard(handCard, cards);
        //删牌结束关闭确认按钮
        this.confirmBtn.active = false;
        this.isconfirm = false;
        //激活原本按钮
        this.deleteBtn.active = true;
        this.addBtn.active = true;
    }
    deleteCard(){
        this.prepare();
        //随机抽牌并显示
        this.cardMgr.reStart();
        let handCard = this.cardMgr.topCard(this.node).children;
        this.addordelete=false;
    }
    addCard(){
        this.prepare();
        //随机创建卡牌
        this.cardbffer=this.cardMgr.CreateCard(this.node);
        this.addordelete=true;
    }
    //前置准备
    prepare(){
        if (GameData.coin < 5) {
            return;
        }
        GameData.coin -= 5;
        //将按钮隐藏防止遮挡
        this.deleteBtn.active = false;
        this.addBtn.active = false;
        //确认激活
        this.isconfirm = true;
    }
}


